package com.xiemulei.gulimall.member.controller;

import com.xiemulei.common.exception.BizCodeEnum;
import com.xiemulei.common.utils.PageUtils;
import com.xiemulei.common.utils.R;
import com.xiemulei.common.vo.SocialUserVo;
import com.xiemulei.common.vo.UserLoginVo;
import com.xiemulei.gulimall.member.entity.MemberEntity;
import com.xiemulei.gulimall.member.exception.PhoneExistsException;
import com.xiemulei.gulimall.member.exception.UsernameExistsException;
import com.xiemulei.gulimall.member.service.MemberService;
import com.xiemulei.gulimall.member.vo.MemberRegisterVo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author xiewenchang
 * @email xiemulei@gmail.com
 * @date 2021-08-06 11:35:23
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegisterVo vo) {
        try {
            memberService.register(vo);
        } catch (UsernameExistsException e) {
            return R.error(BizCodeEnum.USER_EXISTS_EXCEPTION.getCode(), BizCodeEnum.USER_EXISTS_EXCEPTION.getMsg());
        } catch (PhoneExistsException e) {
            return R.error(BizCodeEnum.PHONE_EXISTS_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXISTS_EXCEPTION.getMsg());
        }

        return R.ok();
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo) {
        MemberEntity entity = memberService.login(vo);
        if (entity!=null) {
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnum.ACCOUNT_LOGIN_EXCEPTION.getCode(),BizCodeEnum.ACCOUNT_LOGIN_EXCEPTION.getMsg());
        }

    }

    @PostMapping("/oauth2/login")
    public R oauthLogin(@RequestBody SocialUserVo socialUserVo) throws Exception {
        MemberEntity entity = memberService.login(socialUserVo);
        if (entity!=null) {
            return R.ok().setData(entity);
        }else {
            return R.error(BizCodeEnum.ACCOUNT_LOGIN_EXCEPTION.getCode(),BizCodeEnum.ACCOUNT_LOGIN_EXCEPTION.getMsg());
        }

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
